import request from './request';

/**
    判断当前是否为黑夜模式
    * 白天：false
    * 黑夜：true
*/
export function isBlackMode() {
  return getCssStyle() === 'black';
}

export function getSelfCodeList() {
  return new Promise((resolve, reject) => {
    try {
      window.callNativeHandler('selfCodeAction', { action: 'getAllStocks' }, function (data) {
        resolve(data);
      });
    } catch (error) {
      reject(error);
    }
  });
}

function getCookie(a) {
  const b = a + '=',
    c = document.cookie.indexOf(b);
  if (-1 == c) return null;
  let d = document.cookie.indexOf(';', c + b.length);
  return (
    -1 == d && (d = document.cookie.length),
    window.unescape(document.cookie.substring(c + b.length, d))
  );
}

export function getUserId() {
  if (getCookie('userid')) {
    return getCookie('userid');
  } else {
    const ua = navigator.userAgent;
    if (ua.indexOf('userid/') > -1) {
      return ua.split('userid/')[1].split(' ')[0] || '0';
    } else {
      return '0';
    }
  }
}

export function isIos() {
  return /(iphone|ipad|ipod|ios)/.test(window.navigator.userAgent.toLowerCase());
}

export function newFenshiJump(params) {
  if (params.stockcode == undefined || params.stockcode == null) {
    throw new TypeError('stockcode is null');
  }
  const data = Object.assign(
    {
      stockcode: null,
      marketid: '',
      action: 'ymtz',
      webid: '2205',
      fontzoom: 'no',
    },
    params
  );
  let link =
    'client://client.html?action=ymtz^stockcode=' +
    data.stockcode +
    '^marketid=' +
    data.marketid +
    '^webid=' +
    data.webid +
    '^url=^fontzoom=' +
    data.fontzoom;

  if (data.marketlist != undefined && data.marketlist != null) {
    link = link + '^marketlist=' + `${data.marketlist.join(',')}`;
  }

  if (data.namelist != undefined && data.namelist != null) {
    link = link + '^namelist=' + data.namelist.join(',');
  }

  if (data.codelist != undefined && data.codelist != null) {
    link = link + '^codelist=' + data.codelist.join(',');
  }
  window.location.href = link;
}

//获取连板天梯数据
export async function getContinuousUp(businessId, params) {
  let url = '//dq.10jqka.com.cn/fuyao/ths_widget/limit_up/v1/continuous_limit_up';
  let flag = true;
  for (const key in params) {
    if (flag) {
      url += `?${key}=${params[key]}`;
      flag = false;
    } else {
      url += `&${key}=${params[key]}`;
    }
  }
  const data = await request({
    method: 'get',
    url,
  });
  return data;
}

// 获取服务器时间
export async function getServeTime(businessId) {
  const url = '//dq.10jqka.com.cn/fuyao/ths_widget/limit_up/v1/trade_status';
  const data = await request({
    method: 'get',
    url,
  });
  return data;
}
